/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyDate;

import java.util.*;
import javax.microedition.lcdui.*;
import sUtils.*;

/**
 *
 * @author thomas.s
 */
public class TimeChoice extends Canvas {

    int hh;
    int mm;
    int alarm=0;
    int len=60;
    int lenIndex=5;

    int ox,oy,dx,dy; int slider=0;
    Vector buttons = new Vector(10,2);

    class Button
    {
        public int col=0, val=0;
        public boolean bottom, down;
        public String text, text2;
        int cx,cy,cw,ch;

        public Button( String s, int c, boolean b, int v )
        {
            text=s; bottom=b; col=c; down=c==0; val=v;
            String [] ss = Strings.split(s, ' ');
            text = ss[0];
            text2 = ss.length>1 ? ss[1] : null;
        }

        public void draw( Graphics g )
        {
            int w = getWidth();
            int h = getHeight();
            cw = w/7;
            ch = cw;
            cx = col*cw;
            cy = bottom ? (h-ch) : 0;

            g.setColor( bottom?(down?0x8080c0:0x40):(down?0xc08080:0x400000) );
            g.fillRoundRect(cx+2, cy+2, cw-4, ch-4, cw/4, cw/4);

            g.setColor( bottom?(down?0:0x3030ff):(down?0:0xff3030) );
            Font f = g.getFont();
            if (text2!=null)
            {
                g.drawString(text, cx+(cw-f.stringWidth(text))/2, cy+ch/2-f.getHeight()-1, 0);
                g.drawString(text2, cx+(cw-f.stringWidth(text2))/2, cy+ch/2+1, 0);
            }
            else
                g.drawString(text, cx+(cw-f.stringWidth(text))/2, cy+(ch-f.getHeight())/2, 0);

        }

        public boolean hit( int x, int y )
        {
            if (x>=cx && y>=cy && x<(cx+cw) && y<(cy+ch))
            {   // down = true;
                 return true;
            }
            return false;
        }
    }

    public TimeChoice()
    {
        Calendar c = Calendar.getInstance();
        hh = (c.get(Calendar.HOUR_OF_DAY)+1)%24;
        mm = 0;
        buttons.addElement( new Button(Lang.word("off","aus"),0,false,0));
        buttons.addElement( new Button("5 min",1,false,5));
        buttons.addElement( new Button("30 min",2,false,30));
        buttons.addElement( new Button(Lang.word("2 h","2 Std"),3,false,2*60));
        buttons.addElement( new Button(Lang.word("6 h","6 Std"),4,false,6*60));
        buttons.addElement( new Button(Lang.word("1 day","1 Tag"),5,false,60*24));
        buttons.addElement( new Button(Lang.word("1½ day","1½ Tag"),6,false,60*36));

        buttons.addElement( new Button("--",0,true,0));
        buttons.addElement( new Button("30 min",1,true,30));
        buttons.addElement( new Button(Lang.word("1 h","1 Std"),2,true,60));
        buttons.addElement( new Button(Lang.word("2 h","2 Std"),3,true,2*60));
        buttons.addElement( new Button(Lang.word("4 h day","4 Std"),4,true,4*60));
        buttons.addElement( new Button(Lang.word("1 day","1 Tag"),5,true,24*60));
        buttons.addElement( new Button(Lang.word("2 days","2 Tage"),6,true,48*60));

    }

    void setTimes( int h, int m, int l, int a )
    {
        hh=h; mm=m; len=l; alarm=a;
        for ( int i=0; i<buttons.size(); i++)
        {
            Button b = (Button)(buttons.elementAt(i));
            if (b.bottom) b.down=(b.val==len); else b.down=(b.val==alarm);
        }
        repaint();
    }


    public void paint(Graphics g)
    {
        int w = getWidth();
        int h = getHeight();
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
        int th = f.getHeight();
        g.setFont( f );
        g.setColor( 0 );
        g.fillRect( 0,0,w,h );

        // Zeit:
        for ( int i=-5; i<=5; i++ )
        {
            int c = 255-(i<0?-i:i)*50, y = i*50+h/2-25;

            if (i==0)
            {
                g.setColor(0x40);
                g.fillRoundRect(w/2-100,y-5,200,10+th,20,20);
            }

            g.setColor(i==0?0x8080f0:c);
            StringBuffer s = new StringBuffer();
            int ih = (hh+i+24)%24;
            int im = (mm+i+60)%60;
            if (ih<10) s.append("0"); s.append(ih); s.append(" ");
            int sl = f.stringWidth(s.toString());
            s.append(": ");
            if (im<10) s.append("0"); s.append(im);
            g.drawString(s.toString(), w/2-sl, y, 0);
        }
       f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f);
        for ( int j=0; j<buttons.size(); j++ )
        {
            Button b = (Button)buttons.elementAt(j);
            b.draw(g);
        }
    }

    public void pointerPressed( int x, int y )
    {
        int h = getHeight();
        int w = getWidth();
        if (y>100 && y<(h-100)) slider=1; else slider=0;
        ox = x; oy = y;
        if (slider==0)
        for ( int j=0; j<buttons.size(); j++ )
        {
            Button b = (Button)buttons.elementAt(j);
            if (b.hit(x, y))
            {
                for ( int m=0; m<buttons.size(); m++ )
                {
                    Button bm = (Button)buttons.elementAt(m);
                    if (bm.bottom==b.bottom) bm.down = (b==bm);
                }
                if ( b.bottom ) len = b.val; else alarm = b.val;
                repaint();
                return;
            }
        }
    }

    public void pointerReleased( int x, int y )
    {

    }

    public void pointerDragged( int x, int y )
    {
        if (slider==1)
        {
            int ohh = hh, omm = mm;
            int w2 = getWidth()/2;
            dy -= y-oy; oy=y;

            if (x<w2-5) {
                if (dy>50) { hh = (hh+1)%24; dy-=50; } else
                if (dy<-50) { hh = (hh+23)%24; dy+=50; }
            }
            else if (x>w2+5)
            {
                if (dy>30) { mm = (mm+1)%60; dy-=30; } else
                if (dy<-30) { mm = (mm+59)%60; dy+=30; }
            }
            else
                dy=0;
            if (ohh!=hh || omm!=mm )
            {

                repaint();
            }
        }
    }


}
